/*
 * @Descripttion: 
 * @version: 
 * @@Company: DCIT-SH
 * @Author: 王富银
 * @Date: 2024-08-08 14:59:47
 * @LastEditors: Sunny Chen
 * @LastEditTime: 2024-08-12 15:50:17
 */
const arr = [];
// 生成一个指定范围内的随机时间
function getRandomDate(startDate, endDate) {
  // 将开始时间和结束时间转换为时间戳
  const startTimestamp = startDate.getTime();
  const endTimestamp = endDate.getTime();

  // 生成一个在开始时间和结束时间之间的随机时间戳
  const randomTimestamp =
    startTimestamp + Math.random() * (endTimestamp - startTimestamp);

  // 将随机时间戳转换为 Date 对象
  return new Date(randomTimestamp);
}

const start = new Date("2022-06-05T00:00:00");
const end = new Date();

function formatDate(date) {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, "0");
  const day = String(date.getDate()).padStart(2, "0");
  const hours = String(date.getHours()).padStart(2, "0");
  const minutes = String(date.getMinutes()).padStart(2, "0");
  const seconds = String(date.getSeconds()).padStart(2, "0");

  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

const now = new Date();
console.log(formatDate(now)); // 输出类似 "2024-08-08 15:30:45"
const stateArr = ['正常','故障'];
const faultClass = ['座包损坏','龙头损坏','车架损坏','刹车失灵','其他原因'];
const faultStateArr = ['待处理','处理中'];
for (let i = 2; i <= 33; i++) {
  //id
  let id = "XH";
  if (i < 10) {
    id += "00" + i;
  } else if (i >= 10 && i < 99) {
    id += "0" + i;
  } else {
    id += i;
  }
  //电车的坐标
  const lng = Math.random() * 0.1 + 114.4;
  const lat = Math.random() * 0.1 + 30.4;
  //电量
  let power = (Math.random() * 100).toFixed(2) + "%";
  //状态
  const state = stateArr[Math.floor(Math.random() * stateArr.length)];
  //骑行的里程
  const mileage = (Math.random() * 50).toFixed(2);
  //骑行的次数
  const count = Math.floor(Math.random() * 10);
  //投放时间
  const investDate = formatDate(getRandomDate(start, end));
  const obj = {
    "id":id,
    "coordinates":[lng,lat],
    "state":state,
    "power":power,
    "mileage":mileage,
    "count":count,
    "investDate":investDate,
  };
  //判断是不是故障车辆
  if(obj.state == '故障'){
    obj.faultContent = faultClass[Math.floor(Math.random() * faultClass.length)];
    obj.faultState = faultStateArr[Math.floor(Math.random()*faultStateArr.length)];
    obj.faultDate = formatDate(getRandomDate(start,new Date()));
    obj.faultPerson = 'xxxxxx';
  }
  arr.push(obj);
}
console.log(JSON.stringify(arr));
